package com.ds.home.database.dao;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.database.BaseHibernateDAO;
import com.ds.home.database.vo.ZNodeVO;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.query.NativeQuery;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for Znode
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see ZNodeVO
 * @author MyEclipse Persistence Tools
 */
public class ZNodeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(JDSConstants.CONFIG_KEY, ZNodeDAO.class);
	// property constants
	public static final String DEVICEID = "deviceid";
	public static final String ENDPOINTID = "endpointid";
	public static final String PARENTID = "parentid";
	public static final String PANID = "panid";
	public static final String MACADDRESS = "macaddress";
	public static final String PROFILEPATH = "profilepath";
	public static final String MASTERKEY = "masterkey";
	public static final String NAME = "name";
	public static final String ZMODULEID = "zmoduleid";
	public static final String CREATEUISERID = "createuiserid";




	public  List<ZNodeVO> loadByWhere(String where ){

		NativeQuery<ZNodeVO> query = this.getSession().createNativeQuery("select *  from ha_znode "+where, ZNodeVO.class);
		List<ZNodeVO>  volist= query.getResultList();
		return volist;
	}


	public void update(ZNodeVO transientInstance) {
		log.debug("update Znode instance");
		try {
			    getSession().getTransaction().begin();
				getSession().update(transientInstance);
				getSession().getTransaction().commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public void delete(ZNodeVO persistentInstance) {
		log.debug("deleting Znode instance");
		try {
			  getSession().getTransaction().begin();
			getSession().delete(persistentInstance);
			getSession().getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public ZNodeVO findById(String id) {
		log.debug("getting Znode instance with id: " + id);
		ZNodeVO instance =null;
		try {
			 instance = (ZNodeVO) getSession().get(
					"com.ds.home.database.vo.ZNodeVO", id);
			
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			close();
		}
		return instance;
	}

	public List<ZNodeVO> findByExample(ZNodeVO instance) {
		log.debug("finding Znode instance by example");
		List results =null;
		try {
			results=getSession()
					.createCriteria("com.ds.home.database.vo.ZNodeVO")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Znode instance with property: " + propertyName
				+ ", value: " + value);
		List querList=null;
		try {
			String queryString = "from ZNodeVO as model where model."
					+ propertyName + "= ?0";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			querList= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			close();
		}
		return querList;
	}

	public List<ZNodeVO> findByDeviceid(Object deviceid) {
		return findByProperty(DEVICEID, deviceid);
	}
	public List<ZNodeVO> findByEndPointid(Object endpointid) {
		return findByProperty(ENDPOINTID, endpointid);
	}

	public List<ZNodeVO> findByParentid(Object parentid) {
		return findByProperty(PARENTID, parentid);
	}

	public List<ZNodeVO> findByPanid(Object panid) {
		return findByProperty(PANID, panid);
	}

	public List<ZNodeVO> findByMacaddress(Object macaddress) {
		return findByProperty(MACADDRESS, macaddress);
	}

	public List<ZNodeVO> findByProfilepath(Object profilepath) {
		return findByProperty(PROFILEPATH, profilepath);
	}

	public List<ZNodeVO> findByMasterkey(Object masterkey) {
		return findByProperty(MASTERKEY, masterkey);
	}

	public List<ZNodeVO> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<ZNodeVO> findByZmoduleid(Object zmoduleid) {
		return findByProperty(ZMODULEID, zmoduleid);
	}

	public List<ZNodeVO> findByCreateuiserid(Object createuiserid) {
		return findByProperty(CREATEUISERID, createuiserid);
	}

	public List<ZNodeVO> findAll() {
		log.debug("finding all Znode instances");
		List querList=null;
		try {
			String queryString = "from ZNodeVO";
			Query queryObject = getSession().createQuery(queryString);
			querList= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			close();
		}
		return querList;
	}



	public void insert(ZNodeVO instance) {
		log.debug("attaching dirty ZNodeVO instance");
		try {
		   getSession().getTransaction().begin();
			getSession().insert(instance);
			getSession().getTransaction().commit();
			
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			close();
		}
	}


}